package com.zkx.laolaiyi.service.impl;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 由于需要在常规的信息里面加入role属性，所以需要自定义AuthenticationToken，
 */
public class AuthenticationTokenImpl extends AbstractAuthenticationToken {
    private final Object principal;
    private final Object credentials;
    private final String role;
    public AuthenticationTokenImpl(String username, String password, String role) {
        super(null);
        this.principal = username;
        this.credentials = password;
        this.role = role;
        setAuthenticated(false);
    }
    public AuthenticationTokenImpl(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities, String role) {
        super(authorities); // 设置用户权限
        this.principal = principal;
        this.credentials = credentials;
        this.role = role;
        super.setAuthenticated(true); // 已认证
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    public String getRole() {
        return this.role;
    }
}
